package event.money.tracker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import MTClient.ClientLogic;
import SQLManager.SQLManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainEvents extends Activity {
	
	private SimpleIDAdapter event_adapter = null;
	
	private ProgressDialog loadingpopup = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.main_events);
	    
		Button syncButton = (Button)this.findViewById(R.id.EventListSync);
	   	syncButton.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
        	    setResult(RESULT_OK);
        	    System.out.println("syncing events");
        	    
        	    if( ClientLogic.GetInstance().SendSynLocalDBRequest() ) {
        	    	// popup loading dialog
        	    	loadingpopup = ProgressDialog.show(MainEvents.this, "", 
        	                "Loading. Please wait...", true);
        	    } else
        	    	Toast.makeText(MainEvents.this.getApplicationContext(), "Error: No Internet!",
    	    	            Toast.LENGTH_SHORT).show();
        	    
        	}

        });
	}
	
	public void DrawEvents(){
		
		ArrayList<Map<String, String>> events = new ArrayList<Map<String, String>>();

	    String[] from = { "EventTitle", "EventSubtitle" };  
        int[] to = { R.id.EventItemTitle, R.id.EventItemSubtitle };  
  		
        ArrayList<Integer> idList = new ArrayList<Integer>();

  		SQLManager.GetInstance().open();
  		Cursor EventListCur = SQLManager.GetInstance().FetchEventInfo();
  		if (EventListCur.getCount() > 0) {
  			while (EventListCur.moveToNext()){
  				
  				// get user name
  				String idMaster = EventListCur.getString(EventListCur.getColumnIndex("Master"));
  				String MasterUsername = ClientLogic.GetInstance().GetUserNameFromID(idMaster);
  				
  				Map<String, String> event_item = new HashMap<String, String>();
  				event_item.put("EventTitle", EventListCur.getString(EventListCur.getColumnIndex("Title")));
  				event_item.put("EventSubtitle", "Creator: " + MasterUsername);
  				events.add(event_item);
  				
  				idList.add(EventListCur.getInt(EventListCur.getColumnIndex("ID")));
  			}
  				
  		}	    
  		
  		ListView lv = (ListView)findViewById(R.id.EventListView);
  		
  		event_adapter = new SimpleIDAdapter(this, events, idList, R.layout.event_items, from, to);
	    
  		lv.setAdapter(event_adapter);
	    
	    lv.setTextFilterEnabled(true);

	    lv.setOnItemClickListener(new OnItemClickListener() {
	      public void onItemClick(AdapterView<?> parent, View view,
	          int position, long id) {

	        
	        // start new screen with detailed info and product list
	        Intent intent = new Intent(getApplicationContext(), EventDetails.class);
	        intent.putExtra("eventID", event_adapter.GetUniqueID(position));
	        startActivity(intent);
	        
	      }
	    });
		
	}
	
	protected void onStart()
	{
		super.onStart();
		
		// (re)draw events
		DrawEvents();
	    
	    ClientLogic.SetCurrentActivity(MainEvents.this);
	}
	
	public void handle_sync_result() {
		
		loadingpopup.dismiss();
		
		DrawEvents();
	}

}
